package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vilyever.drawingview.DrawingView;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.bean.MyDrawBean;
import flc.ast.databinding.ActivityNewDrawBinding;
import flc.ast.dialog.ColorDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class NewDrawActivity extends BaseAc<ActivityNewDrawBinding> {

    public static boolean isNew;
    public static boolean isFill;
    public static MyDrawBean drawBean;
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private ColorAdapter brushColorAdapter;
    private int oldColorPos;
    private ColorDialog myColorDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_new_draw;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivDrawBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDrawRevocation.setOnClickListener(this);
        mDataBinding.ivDrawRecover.setOnClickListener(this);
        mDataBinding.ivDrawClear.setOnClickListener(this);
        mDataBinding.ivDrawSave.setOnClickListener(this);
        mDataBinding.llDrawBrushSel.setOnClickListener(this);
        mDataBinding.llDrawEraserSel.setOnClickListener(this);
        mDataBinding.llDrawColorSel.setOnClickListener(this);
        mDataBinding.ivBrushCancel.setOnClickListener(this);
        mDataBinding.ivEraserCancel.setOnClickListener(this);
        mDataBinding.ivColorCancel.setOnClickListener(this);
        mDataBinding.ivColorSel.setOnClickListener(this);

        mDataBinding.myDrawingView.setUndoRedoStateDelegate(new DrawingView.UndoRedoStateDelegate() {
            @Override
            public void onUndoRedoStateChange(DrawingView drawingView, boolean canUndo, boolean canRedo) {
                if (canUndo) {
                    mDataBinding.ivDrawRevocation.setImageResource(R.drawable.chexiao1);
                } else {
                    mDataBinding.ivDrawRevocation.setImageResource(R.drawable.chexiao2);
                }
                if (canRedo) {
                    mDataBinding.ivDrawRecover.setImageResource(R.drawable.cx1);
                } else {
                    mDataBinding.ivDrawRecover.setImageResource(R.drawable.cx2);
                }
            }
        });

        //画笔颜色
        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        brushColorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(brushColorAdapter);
        brushColorAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llDrawBrushSel://画笔
                clearSelection();
                penBrush.setIsEraser(false);
                penBrush.setSize(paintWidth);
                mDataBinding.ivDrawBrushSel.setVisibility(View.VISIBLE);
                mDataBinding.llBrush.setVisibility(View.VISIBLE);
                break;
            case R.id.ivBrushCancel://画笔返回
                mDataBinding.llBrush.setVisibility(View.GONE);
                mDataBinding.llDrawBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.llDrawColorSel://颜色
                clearSelection();
                mDataBinding.ivDrawColorSel.setVisibility(View.VISIBLE);
                mDataBinding.llColor.setVisibility(View.VISIBLE);
                break;
            case R.id.ivColorCancel://颜色返回
                mDataBinding.llColor.setVisibility(View.GONE);
                mDataBinding.llDrawBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.llDrawEraserSel://橡皮
                clearSelection();
                penBrush.setIsEraser(true);
                penBrush.setSize(eraserWidth);
                mDataBinding.ivDrawEraserSel.setVisibility(View.VISIBLE);
                mDataBinding.llEraser.setVisibility(View.VISIBLE);
                break;
            case R.id.ivEraserCancel://橡皮返回
                mDataBinding.llEraser.setVisibility(View.GONE);
                mDataBinding.llDrawBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.ivDrawRecover://复原
                mDataBinding.myDrawingView.redo();
                break;
            case R.id.ivDrawRevocation://撤回
                mDataBinding.myDrawingView.undo();
                break;
            case R.id.ivDrawClear://刷新
                mDataBinding.myDrawingView.clear();
                break;
            case R.id.ivDrawSave://保存
                saveImg();
                break;
            case R.id.ivColorSel://画笔颜色自定义
                myColorDialog.show();
                myColorDialog.setListener(new ColorDialog.IListener() {
                    @Override
                    public void onClickConfirm(String color) {
                        penBrush.setColor(Color.parseColor(color));//默认画笔颜色
                    }
                });
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        brushColorAdapter.getItem(oldColorPos).setSelect(false);
        oldColorPos = position;
        brushColorAdapter.getItem(position).setSelect(true);
        brushColorAdapter.notifyDataSetChanged();
        penBrush.setColor(Color.parseColor(brushColorAdapter.getItem(position).getColor()));//默认画笔颜色
    }

    @Override
    protected void initData() {
        if (isNew) {
            mDataBinding.ivDrawImgBg.setVisibility(View.GONE);
            mDataBinding.ivDrawImg.setVisibility(View.GONE);
        } else {
            mDataBinding.ivDrawImgBg.setVisibility(View.VISIBLE);
            mDataBinding.ivDrawImg.setVisibility(View.VISIBLE);
            mDataBinding.ivDrawImg.setImageResource(drawBean.getSmallImg());
            mDataBinding.ivDrawImgBg.setImageResource(drawBean.getBigImg());
        }
        //画笔颜色
        List<MyColorBean> listBrushColor = new ArrayList<>();
        listBrushColor.add(new MyColorBean("#333333", true));
        listBrushColor.add(new MyColorBean("#D8D8D8", false));
        listBrushColor.add(new MyColorBean("#D0B5A4", false));
        listBrushColor.add(new MyColorBean("#F5C6BF", false));
        listBrushColor.add(new MyColorBean("#FA8EA5", false));
        listBrushColor.add(new MyColorBean("#B17DD2", false));
        listBrushColor.add(new MyColorBean("#A5201B", false));
        listBrushColor.add(new MyColorBean("#5FBC8F", false));
        listBrushColor.add(new MyColorBean("#3F68BC", false));
        listBrushColor.add(new MyColorBean("#9F45F1", false));
        listBrushColor.add(new MyColorBean("#E85C90", false));
        listBrushColor.add(new MyColorBean("#C0C0C0", false));
        brushColorAdapter.setList(listBrushColor);

        //初始化画笔
        initPen();

        myColorDialog = new ColorDialog(mContext);
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#333333"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小
        mDataBinding.seekbarBrush.setMax(50);
        mDataBinding.seekbarBrush.setProgress(5);
        mDataBinding.seekbarBrush.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //设置橡皮擦大小
        mDataBinding.seekbarEraser.setMax(50);
        mDataBinding.seekbarEraser.setProgress(5);
        mDataBinding.seekbarEraser.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        if (!isNew) {
            mDataBinding.ivDrawImg.setVisibility(View.GONE);
            if (!isFill) {
                mDataBinding.ivDrawImgBg.setVisibility(View.GONE);
            }
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlDrawSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        //保存到历史记录
                        String filePath = FileUtil.generateFilePath(Content.myCreativeRecord, ".png");
                        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_to_album);
                        finish();
                    }
                });
            }
        }, 1000);
    }

    private void clearSelection() {
        mDataBinding.ivDrawBrushSel.setVisibility(View.GONE);
        mDataBinding.ivDrawColorSel.setVisibility(View.GONE);
        mDataBinding.ivDrawEraserSel.setVisibility(View.GONE);
        mDataBinding.llDrawBottom.setVisibility(View.GONE);
        mDataBinding.llBrush.setVisibility(View.GONE);
        mDataBinding.llColor.setVisibility(View.GONE);
        mDataBinding.llEraser.setVisibility(View.GONE);
    }

}